import { request } from '../../utils/request'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 导航标签数据
        songsGroupList:[],
        // 导航的标识
        navId:'',
        musicList:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function () {
      let songsGroupListData= await request('/category')
      this.setData({
          songsGroupList:songsGroupListData.data,
          navId:songsGroupListData.data[0].id
      })
      let navId = this.data.navId
      request(`/music/${+navId}`).then((result)=>{
        console.log(result);
        this.setData({
            musicList:result.data
        })
        setTimeout(()=>{
          wx.hideLoading()
        },100)
       })
    },
    // 点击切换导航的回调
    changeNav(event){
      let navId=event.currentTarget.id
        wx.showLoading({
          title:'正在加载'
        })
       request(`/music/${+navId}`).then((result)=>{
        console.log(result);
        this.setData({
            musicList:result.data
        })
        setTimeout(()=>{
          wx.hideLoading()
        },100)
       })
        this.setData({
            navId:navId*1
        })
    },
    // 跳转至搜索页面
    toSearch(){
        wx.navigateTo({
          url: '/pages/search/search',
        })
    },
    toSongDetail(e){
      let index = e.currentTarget.dataset.index
      let currentMusic =  this.data.musicList[index]
      wx.navigateTo({
        url: '/pages/songDetail/songDetail',
        success: function(res) {
          res.eventChannel.emit('detail', { musicData:currentMusic})
        }
      })
  },
})